package com.hsmpay.mobile.action.trans;

import com.hsmpay.common.pojo.user.User;
import com.hsmpay.common.util.DateConvertUtil;
import com.hsmpay.common.util.NumberUtil;
import com.hsmpay.common.util.OrderTypeUtil;
import com.hsmpay.mobile.action.account.officialAccPay.OfficialAccPayBaseAction;
import com.hsmpay.mobile.action.base.MobileBaseAction;
import com.hsmpay.mobile.pojo.trans.TransOrder;
import com.hsmpay.mobile.service.trans.TransOrderService;

import javax.annotation.Resource;

import java.security.SecureRandom;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 交易父类 action
 *
 * @author 颜铃璋
 * @date 2013-10-31
 */
public class TransBaseAction extends MobileBaseAction {
    private static final long serialVersionUID = -6951441016122696397L;
    //private static Logger log = Logger.getLogger(PeakCBaseAction.class);
    public static final long RECEIVABLES = OrderTypeUtil.RECEIVABLES;//我要收款
    public static final long PAY = OrderTypeUtil.PAY;//我要付款
    public static final long CARD_TRANSFER = OrderTypeUtil.CARD_TRANSFER;//卡卡转账
    public static final long CREDIT_REPAYMENT = OrderTypeUtil.CREDIT_REPAYMENT;//信用卡还款
    public static final long ENQUIRY = OrderTypeUtil.ENQUIRY;//查询订单
    public static final long REVOCATION = OrderTypeUtil.REVOCATION;//撤销
    public static final long MOBILE_CHARGE = OrderTypeUtil.MOBILE_CHARGE;//话费充值

    public static final double RECEIVABLES_MIN_MONEY = 20.00;//最下金额
    public static final double RECEIVABLES_MAX_MONEY = 50000.00;//最大金额

    public static final long SECONDSTO = OrderTypeUtil.SECONDSTO;//秒到收款
    public static final long ZEROFLPAY = OrderTypeUtil.ZEROFLPAY;//零利率收款

    //----订单服务-----
    @Resource(name = "transOrderService")
    protected TransOrderService<TransOrder, Long> transOrderService;

    
    /**
     *  根据code 获取对应的订单号
	 *  lishijie
	 * @param code
	 */
	public static String genOrderNo(Long code) {
		
		if (code == OfficialAccPayBaseAction.TC_SHANGLIANCHU
                || code == OfficialAccPayBaseAction.TC_JQ
                || code == OfficialAccPayBaseAction.TC_JQ_SECOND
                || code == OfficialAccPayBaseAction.TC_JQ_QR
                || code == OfficialAccPayBaseAction.TC_HSM
                || code == OfficialAccPayBaseAction.TC_JQ_SDK) {
			return getCurrentTimeOrderNo();
		}
		
		return getOrderNumByDate();
	}
	
	/**
	 * 默认的银行订单号的生成规则(20位)
	 * lishijie
	 * @param args
	 */
	public static String genBankOrderNo() {
		return System.currentTimeMillis()
				+ generateRandomDigitString(7);
	}
	
	
	/**
	 * 产生指定长度的无规律数字字符串
	 * @param aLength  生成的随机数的长度
	 * @return			   生成的随机字符串
	 * throws 卡号生成异常
	 */
	private static String generateRandomDigitString(int aLength) {
		SecureRandom 	tRandom  	= new SecureRandom();
		long   			tLong;
		String			aString  	= "";
		
		tRandom.nextLong();
		tLong = Math.abs(tRandom.nextLong());
		aString = (String.valueOf(tLong)).trim();
		while (aString.length() < aLength){
			tLong = Math.abs(tRandom.nextLong());
			aString += (String.valueOf(tLong)).trim();
		}
		aString = aString.substring(0, aLength);
		
		return aString;
	}
	/**
     * 20151205100131454  取到毫秒
     * 
     * @param date as 2014-09-08
     *            字符串形式的日期
     * @throws ParseException 
     */
    public static String currentTime(){
    	SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    	Date currentTime = new Date();//得到当前系统时间
    	return formatter.format(currentTime);
    }
	//2015 1205 1001 3145 4  取到毫秒  + 3位 随机数  20位
	public static String getCurrentTimeOrderNo() {
		return currentTime() + generateRandomDigitString(3);
	}

    /**
     * 28 位 获得订单编号
     *
     * @param date
     * @return
     */
    protected static String getOrderNumByDate() {
        return currentTime() + generateRandomDigitString(11);
    }
    /**
     * 获得订单编号
     *
     * @param date
     * @return
     */
    protected String getOrderNumByDate(Date date) {
        StringBuilder sb = new StringBuilder();
        String dt = DateConvertUtil.dateString(date, "yyyyMMddHHmmss");//获得日期字符串
        String userIdStr = NumberUtil.supplyAll(userId, 10);//获得10位userId
        long r = (long) (Math.random() * 10000);//获得四位随机数
        String rStr = NumberUtil.supplyAll(r, 4);
        return sb.append(dt).append(userIdStr).append(rStr).toString();
    }

    private String[] testUser = {"18210077042", "15811514005", "18600676076", "15711102000", "13810525047", "13366990789", "18688693231", "13521856049", "13901357965", "15811514002", "13810084248", "12888886888", "18510082588", "173665683920003"};

    //金额上限 0 金额小于10  1 通过  2 金额大于30000
    public int checkMoneyMinMax(User user, double money, Long orderType) {
        int flag = 1;
        boolean isTest = false;
        for (String mobile : testUser) {
            if (mobile.equals(user.getMobile())) {
                isTest = true;
                break;
            }
        }

        double min = RECEIVABLES_MIN_MONEY;
        double max = RECEIVABLES_MAX_MONEY;
        //if(type == )
        //if(!isTest){//不是测试数据
        if (!isTest && money < min) {
            flag = 0;
        } else if (money > max) {
            flag = 2;
        }
        //}
        return flag;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    /**
     * 当天 23:00:00
     *
     * @return
     */
    public Date getInitEndDateTime(Date date) {
        String dateStr = DateConvertUtil.dateString(date) + " 23:00:00";
        return DateConvertUtil.stringDateTime(dateStr);
    }

    /**
     * 前一天 23:00:00
     *
     * @return
     */
    public Date getInitStartDateTime(Date date) {
        Date dayBefore = DateConvertUtil.getSpecifiedDayBefore(date);//指定日期  前一天
        String dateStr = DateConvertUtil.dateString(dayBefore) + " 23:00:00";
        return DateConvertUtil.stringDateTime(dateStr);
    }
}
